#include "StdAfx.h"
#include "ButtonUI.h"
#include "BlueRenderEngineUI.h"

CButtonUI::CButtonUI(void): m_cxWidth(0), m_uButtonState(0), m_uTextStyle(DT_SINGLELINE | DT_CENTER)
{
	m_szPadding.cx = 4;
	m_szPadding.cy = 0;
}

CButtonUI::~CButtonUI(void)
{
}

//CStdString CButtonUI::GetName() const;
//{
//	return _T("button");
//}
LPCTSTR CButtonUI::GetClass() const
{
	return _T("ButtonUI");
}

UINT CButtonUI::GetControlFlags() const
{
	return UIFLAG_TABSTOP;
}

void CButtonUI::Event(TEventUI& event)
{
	if( event.Type == UIEVENT_BUTTONDOWN || event.Type == UIEVENT_DBLCLICK )
	{
		if( ::PtInRect(&m_rcItem, event.ptMouse) && IsEnabled() ) {
			m_uButtonState |= UISTATE_PUSHED | UISTATE_CAPTURED;
			Invalidate();
		}
	}
	if( event.Type == UIEVENT_MOUSEMOVE )
	{
		if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
			if( ::PtInRect(&m_rcItem, event.ptMouse) ) m_uButtonState |= UISTATE_PUSHED;
			else m_uButtonState &= ~UISTATE_PUSHED;
			Invalidate();
		}
	}
	if( event.Type == UIEVENT_BUTTONUP )
	{
		if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
			if( ::PtInRect(&m_rcItem, event.ptMouse) ) Activate();
			m_uButtonState &= ~(UISTATE_PUSHED | UISTATE_CAPTURED);
			Invalidate();
		}
	}
	if( event.Type == UIEVENT_MOUSEENTER)
	{
		m_uButtonState |= UISTATE_HOT;
		Invalidate();
	}
	if( event.Type == UIEVENT_MOUSELEAVE)
	{
		m_uButtonState &= ~UISTATE_HOT;
		Invalidate();
	}
	CControlUI::Event(event);
}

void CButtonUI::SetText(LPCTSTR pstrText)
{
	CControlUI::SetText(pstrText);
	// Automatic assignment of keyboard shortcut
	if( _tcschr(pstrText, '&') != NULL ) m_chShortcut = *(_tcschr(pstrText, '&') + 1);
}

bool CButtonUI::Activate()
{
	if( !CControlUI::Activate() ) return false;
	if( m_pManager != NULL ) m_pManager->SendNotify(this, _T("click"));
	return true;
}

void CButtonUI::SetWidth(int cxWidth)
{
	m_cxWidth = cxWidth;
	UpdateLayout();
}

void CButtonUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
{
	if( _tcscmp(pstrName, _T("width")) == 0 )
		SetWidth(_ttoi(pstrValue));
	else if( _tcscmp(pstrName, _T("align")) == 0 ) 
	{
		if( _tcsstr(pstrValue, _T("center")) != NULL ) 
			m_uTextStyle |= DT_CENTER;
		if( _tcsstr(pstrValue, _T("right")) != NULL )
			m_uTextStyle |= DT_RIGHT;
		if( _tcsstr(pstrValue, _T("vcenter")) != NULL )
			m_uTextStyle |= DT_VCENTER;

	}
	else CControlUI::SetAttribute(pstrName, pstrValue);
}

void CButtonUI::SetPadding(int cx, int cy)
{
	m_szPadding.cx = cx;
	m_szPadding.cy = cy;
	UpdateLayout();
}

SIZE CButtonUI::EstimateSize(SIZE /*szAvailable*/)
{
	SIZE sz = { m_cxWidth, 12 + m_pManager->GetThemeFontInfo(UIFONT_NORMAL).tmHeight };
	if( m_cxWidth == 0 && m_pManager != NULL ) {
		RECT rcText = { 0, 0, 9999, 20 };
		int nLinks = 0;
		CBlueRenderEngineUI::DoPaintPrettyText(m_pManager->GetPaintDC(), m_pManager, rcText, m_sText, 
			UICOLOR_STANDARD_BLACK, UICOLOR__INVALID, NULL, nLinks, DT_SINGLELINE | DT_CALCRECT);
		sz.cx = rcText.right - rcText.left;
	}
	sz.cx += m_szPadding.cx * 2;
	sz.cy += m_szPadding.cy * 2;
	return sz;
}

void CButtonUI::DoPaint(HDC hDC, const RECT& rcPaint)
{
	// Draw button
	UINT uState = 0;
	if( IsFocused() ) uState |= UISTATE_FOCUSED;
	if( !IsEnabled() ) uState |= UISTATE_DISABLED;
	RECT rcPadding = { m_szPadding.cx, m_szPadding.cy, m_szPadding.cx, m_szPadding.cy };
	CBlueRenderEngineUI::DoPaintButton(hDC, m_pManager, m_rcItem, m_sText, rcPadding, m_uButtonState | uState, m_uTextStyle);
}

RECT CButtonUI::GetPos() const
{
	return m_rcItem;
}

void CButtonUI::SetPos(RECT rc)
{
	m_rcItem = rc;
	Invalidate();
}
